package com.loong.web.gp4j.component.intercept;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.loong.web.gp4j.bean.schema.user.UserSchema;
import com.loong.web.gp4j.bean.schema.user.UserStatuses;
import com.loong.web.gp4j.dao.user.UserDao;
import com.loong.web.gp4j.service.user.UserAdapter;
import com.loong.web.suppert.auth.User;
import com.loong.web.suppert.collection.list.KvnList;
import com.loong.web.suppert.intercept.cookie.CookieDataReader;

/**
 * Cookie数据读取组件
 *
 * @author 张成轩
 */
public class CookieReaderComponent implements CookieDataReader {

	@Resource
	private UserDao userDao;
	@Resource(name = "code.auths")
	private KvnList<String, Integer, String> auths;
	@Resource(name = "user.statuses")
	private KvnList<String, Integer, String> statuses;

	@Override
	public User getUser(HttpServletRequest request, String uid) {

		Integer userId = Integer.parseInt(uid);
		UserSchema user = userDao.getWithAuths(userId);
		if (user == null || !user.getStatus().equals(statuses.getV(UserStatuses.NORMAL)))
			return null;
		return UserAdapter.build(user, auths);
	}
}
